﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Controls.DataVisualization.Charting;
using ActivityTracker.Library;
using Common.Library;


namespace ActivityTracker
{
    public partial class Yearly_CategoryBarChart : PhoneApplicationPage
    {
        
          #region Global

        Charts objCharts = new Charts();
        YearlyChart objYearlyChart = new YearlyChart();
        string year = "";

        #endregion

        #region constructor

        public Yearly_CategoryBarChart()
        {
            InitializeComponent();
            BarChart.LegendStyle = ChartLegendStyles.hidelegend();
            BarChart.Visibility = Visibility.Collapsed;
        }

        #endregion

        #region Page events

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            string selectedYear = null;
            try
            {
                lpicYear.ItemsSource = objCharts.GetYears();
                lpicYear.SelectedItem = DateTime.Now.ToString("yyyy");
                if (NavigationContext.QueryString.TryGetValue("selectedYear", out selectedYear))
                {
                    lpicYear.SelectedIndex = Convert.ToInt32(selectedYear);
                    year = lpicYear.SelectedItem.ToString();
                }
            }
            catch (Exception ex)
            {
                string mess = "Exception in PhoneApplicationPage_Loaded Method In Yearly_TimespentBarChart file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void btnPieChart_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/Charts/Yearly/Yearly_CategoryPieChart.xaml?SelectedYearindex=" + lpicYear.SelectedIndex.ToString(), UriKind.Relative));
        }

        private void YearListPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lpicYear.SelectedIndex == -1)
                return;
            LoadChart(lpicYear.SelectedItem.ToString());
        }
        //private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
        //{
        //    NavigationService.Navigate(new Uri("/Views/Charts/Yearly/Yearly_TimespentBarChart.xaml", UriKind.Relative));
        //}
        #endregion

        #region Custom

        private void LoadChart(string year)
        {
            try
            {
                List<ChartProp> objChartList = objYearlyChart.LoadCategoryTimeSpentChartdata(year, ChartType.Bar);
                if (objChartList.Count > 0)
                    tblkResult.Visibility = Visibility.Collapsed;
                else
                    tblkResult.Visibility = Visibility.Visible;
                ContentPanel.DataContext = objChartList;
                BarChart.Height = (objChartList.Count * 50) + 50;
                BarSeries b = (BarSeries)BarChart.Series[0];
                CategoryAxis ca = new CategoryAxis();
                ca.Height = objChartList.Count * 50;
                b.IndependentAxis.DependentAxes.Add(ca);
                BarChart.Style = Application.Current.Resources["PhoneChartStyle"] as Style;
                BarChart.Visibility = Visibility.Visible;
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in LoadChart Method In Yearly_TimespentBarChart file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace);
                exNew.Data.Add("year_LoadChart", year);
                throw exNew;
            }
        }

        #endregion

       
    }
}